﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace SscLib
{
    /// <summary>
    /// Defines a local Litteral symbol.
    /// </summary>
    public class LitteralSymbol : Symbol
    {
        /// <summary>
        /// Enumerates the different Litteral kinds allowed.
        /// </summary>
        public enum Kind { INT, CHAR, REAL, STRING, BOOL };

        /// <summary>The Litteral constant value.</summary>
        public object value;

        /// <summary>The Litteral kind.</summary>
        public Kind kind;

        /// <summary>
        /// Creates a Label symbol. Parses the constant value as a string and converts to appropriate type given by the Kind specification.
        /// </summary>
        /// <param name="kind">Litteral kind.</param>
        /// <param name="value">Litteral constant value.</param>
        public LitteralSymbol(Kind kind, string value) : base(kind.ToString(), null)
        {
            this.kind = kind;

            switch (kind)
            {
                case Kind.BOOL:
                    this.value = bool.Parse(value);
                    this.Type = SymbolTable.Boolean;
                    break;
                case Kind.INT:
                    this.value = int.Parse(value);
                    this.Type = SymbolTable.Integer;
                    break;
                case Kind.CHAR:
                    this.value = value[1];
                    this.Type = SymbolTable.Character;
                    break;
                case Kind.REAL:
                    this.value = Single.Parse(value, CultureInfo.InvariantCulture);
                    this.Type = SymbolTable.Real;
                    break;
                case Kind.STRING:
                    this.value = value.TrimStart('"').TrimEnd('"');
                    this.Type = SymbolTable.String;
                    break;
                default:
                    throw new ArgumentException("Uknown Litteral kind '" +kind.ToString() + "'." , "kind");
            }
        }
    }
}
